//
//  HYInfoView.m
//  HaiYue
//
//  Created by skyline on 2018/2/6.
//  Copyright © 2018年 skyline. All rights reserved.
//

#import "HYInfoLabel.h"
@interface HYInfoLabel ()
{
    CGFloat total_img_map;//图片上下间距
}
@property (nonatomic, strong) HYView *topLine;
@property (nonatomic, strong) HYView *bottomLine;
@property (nonatomic, strong) UILabel *label;
@property (nonatomic, strong) NSMutableArray *imageurlArray;

@end
@implementation HYInfoLabel
- (instancetype)init
{
    self = [super init];
    if (self)
    {
        self.label.numberOfLines = 0;
        self.userInteractionEnabled = NO;
    }
    return self;
}

- (UILabel *)label
{
    if (!_label)
    {
        _label = [[UILabel alloc] init];
        _label.userInteractionEnabled = NO;
        [self addSubview:_label];
        [_label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.topLine.mas_bottom);
            make.left.equalTo(self).with.offset(GAP);
            make.right.equalTo(self).with.offset(-GAP);
            make.bottom.equalTo(self.bottomLine.mas_top);
        }];
    }
    return _label;
}

- (HYView *)topLine
{
    if (!_topLine)
    {
        _topLine = [[HYView alloc] initWithColor:CELL_LINE_COLOR];
        [self addSubview:_topLine];
        [_topLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self);
            make.left.equalTo(self);
            make.right.equalTo(self);
            make.height.mas_equalTo(GAP / 2.0);
        }];
    }
    return _topLine;
}

- (HYView *)bottomLine
{
    if (!_bottomLine)
    {
        _bottomLine = [[HYView alloc] initWithColor:CELL_LINE_COLOR];
        [self addSubview:_bottomLine];
        [_bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self);
            make.left.equalTo(self);
            make.right.equalTo(self);
            make.height.mas_equalTo(GAP / 2.0);
        }];
    }
    return _bottomLine;
}

- (void)setHtml:(NSString *)html
{
    if (_html != html)
    {
        _html = html;
        NSString *newHTML = [_html autoHTMLIMGSizeWithWidth:SCREEN_WIDTH  - GAP * 2];
        self.label.attributedText = [[NSAttributedString alloc] initWithData:[newHTML dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nil error:nil];
        
        self.imageurlArray = [[_html urlFromHTML] mutableCopy];
        
        [self updateBoundes];
    }
}

- (void)setImageurlArray:(NSMutableArray *)imageurlArray
{
    if (_imageurlArray != imageurlArray)
    {
        _imageurlArray = imageurlArray;
        total_img_map = _imageurlArray.count *2* GAP;
    }
}

- (void)updateBoundes
{
    CGFloat total_img_H  = [_html totalHTMLIMGHeightWithWidth:SCREEN_WIDTH - GAP * 2.0];
    CGRect rect = (_html.length>0)?[_html getHeightWithWidth:SCREEN_WIDTH - 20 withFont:self.label.font]:CGRectMake(0, 0, 0, GAP);
    self.bounds = CGRectMake(0, 0, SCREEN_WIDTH, rect.size.height + total_img_H + total_img_map);
    
    [self mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(self.bounds.size.height);
    }];
    
}
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
